package models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.util.Date;

/**
 * 文章
 * 
 * @author Cheng 2014/9/20
 */
@Entity
@Table(name = "article")
public class Article extends BasicModel {
	//type
	public static int DRAFT = 0;//草稿
	public static int ACTIVITY = 1;//活动
	public static int BRAND_ = 2;//品牌平面
	public static int BRAND_SURFACE = 3;//品牌文章
	public static int PRODUCT_CONCEPT = 4;//产品概念
	
	
	// 标题
	@Column(name = "title")
	public String title;
	
	//状态    发布或者草稿
	@Column(name = "status")
	public int status;

	// 存简介内容
	@Column(name = "description", columnDefinition = "text")
	public String description;

	// 存富文本内容
	@Column(name = "content", columnDefinition = "text")
	public String content;

	// 作者Id
	@Column(name = "author_id")
	public String authorId;
	
	//BigTag
	@Column(name = "bigTagId")
	public String bigTagId;
	
	//tag
	@Column(name = "tag")
	public String tag;
	
	//版权
	@Column(name = "copyRight")
	public String copyRight;
	
	//是否允许评论
	@Column(name = "is_permit_comment",columnDefinition = "int default 0")
	public int is_permit_comment;

    //种类
    @Column(name = "type", columnDefinition = "int default 0", nullable = false)
    public int type;
    
    //文章的点击量
    @Column(name = "click",columnDefinition = "int default 0")
    public int click;
    
    //评论数
    @Column(name = "commentNum",columnDefinition = "int default 0")
    public int commentNum;
    
    //点赞数
    @Column(name = "praiseNum",columnDefinition = "int default 0")
    public int priseNum;

	// 文章封面
	@Column(name = "photo_id")
	public String photoId;
	
	@Column(name = "attachment_id")
	public String attachmentId;

	// 文章的权重,0最大
	@Column(name = "weight", columnDefinition = "int default 0", nullable = false)
	public int weight;
	
	//是否审核
	@Column(name = "is_verify",columnDefinition = "int default 0")
	public int is_verify;

	// 获取文章封面路径
	public String getPhotoSrc() {
		Photo photo = Photo.find("id = ?", this.photoId).first();
		String path = null;
		if (photo == null) {
			path = "";
		} else {
			path = photo.getPhoto();
		}
		return path;
	}
	
	// 获取文章封面路径
		public String getZipPhotoSrc() {
			Photo photo = Photo.find("id = ?", this.photoId).first();
			String path = null;
			if (photo == null) {
				path = "";
			} else {
				path = photo.getPhoto();
			}
			return path;
		}

	// 更改文章状态
	public void changeStatus() {
		if (this.isDelete == 0) {
			this.isDelete = 1;
		} else {
			this.isDelete = 0;
		}
		this.save();
	}

	// 获取文章作者
	public String getAuthorName() {
		User ui = User.findById(this.authorId);
		if (ui == null)
			return "佚名";
		return ui.nickname;
	}

	// 获取格式化的创建时间
	public String getFormatTime() {
		Date date = new Date(this.createTime);
		return DateFormat.getDateInstance(DateFormat.DEFAULT).format(date);
	}

	// ------------------------------------
	// 静态方法
	public static Article getArticleById(String id) {
		Article article = Article.find("id = ?", id).first();
		return article;
	}
	
	public static String getAuthorIdById(String articleId)
	{
		Article article = Article.findById(articleId);
		if(article != null)
			return article.authorId;
		else
			return null;
	}

}
